#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Description : 封装sql增删查改函数

from commons.choose_db import ConnDB
from commons.logger import log


class DataBase:
    def __init__(self, note: str) -> None:
        self.db = ConnDB(note)

    def del_merchant_config(self, merchant_id):
        """售前权益-开通配置--删除配置"""
        self.db.exec(
            f"delete from service_merchant_config where merchant_id={merchant_id};"
        )
        self.db.exec(
            f"delete from service_merchant_package_config where merchant_id={merchant_id};"
        )

    def update_equity_order_status(self,
                                   contract_No,
                                   status,
                                   expiration_date="2021-06-28 23:59:59"):
        """更新售前权益-订单状态"""
        self.db.exec(
            f"UPDATE service_equity_order SET order_status={status}, expiration_date='{expiration_date}' WHERE contract_no='{contract_No}';"
        )

    def reset_equity_card(self, mobile):
        """更新智慧车小程序端权益卡状态"""
        self.db.exec(
            f"UPDATE equity_card SET deleted_flag='D' WHERE mobile={mobile};")

    def get_equity_card_list(self, merchant_id):
        """运营版-会员权益-权益卡列表"""
        log.debug("当前的商户号是：")
        log.debug(merchant_id)
        log.debug(
            f"select * FROM equity_card_class WHERE merchant_id={merchant_id};")
        query_result = self.db.query(
            f"select * FROM equity_card_class WHERE merchant_id={merchant_id};")

        log.debug(f"查询结果为：{query_result}")
        return query_result

    def del_equity_card(self, merchant_id):
        """运营版-会员权益-权益卡管理-删除权益卡"""
        log.debug("当前的商户号是：")
        log.debug(merchant_id)
        log.debug(
            f"DELETE FROM equity_card_class WHERE merchant_id={merchant_id};")
        self.db.exec(
            f"DELETE FROM equity_card_class WHERE merchant_id={merchant_id};")

    def del_equity_car_service(self, equity_id):
        """运营版-会员权益-权益管理-删除汽车权益"""
        self.db.exec(
            f"UPDATE equity_car_service SET deleted_flag='D' WHERE id={equity_id};"
        )

    def select_equity_order(self, table_name, order_code):
        """查询订单信息，返回查询结果
        :param table_name: 表名
        :param order_code: 订单号
        :return:
        """
        result = self.db.query(
            f"SELECT * FROM {table_name} WHERE order_code = \"{order_code}\"")
        return result
